<?php
include("model/brands.php");
class brandsController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_brands_box = $this->getListBrandBox();
		$smarty->assign("list_brands_box",$list_brands_box);
		
		$content = $smarty->fetch("brands/brands.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListBrandBox(){
		$smarty 	= $_SESSION['smarty'];
		$brand_obj 	= new brands();
		$brandList	= $brand_obj->getBrandsList();		
		$smarty->assign("brandList", $brandList);
		$html 		= $smarty->fetch("brands/brands_list.html");
		return $html;
	}
	
	function insertAction(){
		$brands_obj = new brands();
		$data 		= array();
		$data['br_name'] = $_REQUEST['br_name'];
		$result = $brands_obj->insertABrand($data);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("brands/brands_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$br_id = $_REQUEST['args'];
		$brands_obj = new brands();
		$result = $brands_obj->inactivateABrand($br_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_brands_box = $this->getListBrandBox();
		$smarty->assign("list_brands_box",$list_brands_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("brands/brands.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$br_id = $_REQUEST['args'];
		$brands_obj = new brands();
		$result = $brands_obj->activateABrand($br_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_brands_box = $this->getListBrandBox();
		$smarty->assign("list_brands_box",$list_brands_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("brands/brands.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];		
		
		$insertform_brands_box = $smarty->fetch("brands/brands_insert_form.html");
		$smarty->assign("insertform_brands_box",$insertform_brands_box);
		
		$content = $smarty->fetch("brands/brands.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$br_id 			= $_REQUEST['args'];
		$smarty 		= $_SESSION['smarty'];		
		
		$brands_obj 	= new brands();
		$current_data 	= array();
		$current_data 	= $brands_obj->getBrandsInfo($br_id);
		
		$smarty->assign("current_data", $current_data);
		
		$editform_brands_box = $smarty->fetch("brands/brands_edit_form.html");
		$smarty->assign("editform_brands_box",$editform_brands_box);
		
		$content = $smarty->fetch("brands/brands.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
		
	}
	
	function editAction(){
		$brands_obj = new brands();
		$data 		= array();
		$data['br_name'] 	= $_REQUEST['br_name'];
		$data['br_id'] 		= $_REQUEST['br_id'];
		
		$result = $brands_obj->editABrand($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("brands/brands_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	/*
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		//khoi tao 1 mang
		$arr	= array();
		//gan 1 vai gia tri
		$arr[]	= "nguyen khang";
		$arr[]	= "hoai anh";
		
		//assign mang arr vao smarty
		$smarty->assign("sinhvien", $arr);
		$smarty->display("home.html");	
	}
	
	function xuliAction(){
		//neu button submit duoc bam
		/*
		if (isset($_POST['submit']))
		{
			//chuan bi lai gia tri cho combo box
			$arr	= array();			
			$arr[]	= "nguyen khang";
			$arr[]	= "hoai anh";
			
			//gan gia tri cua combo box vao smarty
			$smarty = $_SESSION['smarty'];
			$smarty->assign("sinhvien", $arr);
			
			//lay gia tri tu $_POST va gan vao 1 bien smarty ten la tensinhvien
			$smarty->assign("tensinhvien", $_POST["sinhvien_select"]);
			
			//display file home.html again, nhung lan nay co gia tri cua bien tensinhvien
			$smarty->display("home.html");	
		}
		
		
		//mot cach viet khac
		if (isset($_POST['submit']))
		{
			//lay gia tri tu $_POST va gan vao 1 bien smarty ten la tensinhvien
			$smarty = $_SESSION['smarty'];
			$smarty->assign("tensinhvien", $_POST["sinhvien_select"]);
			
			$this->indexAction();
		}*/	
	
}
?>